package ac.you.api.chat;

import ac.you.api.chat.i.DomainText;
import com.google.common.collect.Lists;
import com.google.gson.Gson;
import org.bukkit.Bukkit;
import org.bukkit.entity.Player;

import java.util.Collections;
import java.util.LinkedList;
import java.util.List;
import java.util.logging.Logger;

public final class Tellraw {
    public static Tellraw create(){
        return new Tellraw();
    }
    private final LinkedList<Text> texts;
    public Tellraw(){
        this.texts = Lists.newLinkedList();
    }
    public Text text(String content){
        Text var1 = new DomainText(this,content);
        add(var1);
        return var1;
    }
    public Tellraw text(Tellraw t){
        texts.addAll(t.ts());
        return this;
    }
    public void send(Player player){
        send(player,false);
    }
    public void send(Player... players){
        for (Player player : players) {
            send(player);
        }
    }
    public void send(Player player,boolean newline){
        Gson gson = new Gson();
        String command;
        if (newline){
            for (Text text : texts) {
                command = "tellraw " + player.getName() + " " + gson.toJson(text);
                Bukkit.dispatchCommand(Bukkit.getConsoleSender(),command);
            }
        } else {
            command = "tellraw " + player.getName() + " " + gson.toJson(texts);
            Bukkit.dispatchCommand(Bukkit.getConsoleSender(),command);
            Logger.getLogger("AcYouAPI").info("[AcYouAPI-command-tellraw] " + command);
        }

    }
    public void send(boolean newline,Player... players){
        Gson gson = new Gson();
        String command;
        for (Player player : players) if (newline){
            for (Text text : texts) {
                command = "tellraw " + player.getName() + " " + gson.toJson(text);
                Bukkit.dispatchCommand(Bukkit.getConsoleSender(),command);
            }
        } else {
            command = "tellraw " + player.getName() + " " + gson.toJson(texts);
            Bukkit.dispatchCommand(Bukkit.getConsoleSender(),command);
            Logger.getLogger("AcYouAPI").info("[AcYouAPI-command-tellraw] " + command);
        }
    }
    public void add(Text text){
        texts.add(text);
    }
    public List<Text> ts(){
        return Collections.unmodifiableList(texts);
    }


}
